package com.powervision.mvvmdemo

import androidx.activity.viewModels
import by.kirich1409.viewbindingdelegate.viewBinding
import com.powervision.base.base.BaseActivity
import com.powervision.base.util.LogUtils
import com.powervision.mvvmdemo.databinding.ActivityMainBinding
import com.powervision.mvvmdemo.ui.viewmodel.ListProjectViewModel
import com.powervision.mvvmdemo.ui.viewmodel.*

class MainActivity : BaseActivity(R.layout.activity_main) {
    private val mBinding by viewBinding(ActivityMainBinding::bind)
    private val mViewModel by viewModels<ArtListViewModel>()
    private val mKnowViewModel by viewModels<KnowLedgeViewModel>()
    private val mBannerViewModel by viewModels<BannerViewModel>()
    private val mProjectViewModel by viewModels<ProjectViewModel>()
    private val mProjectListViewModel by viewModels<ProjectListViewModel>()
    private val mNaviVideModel by viewModels<NaviViewModel>()
    private val mSearchViewModel by viewModels<SearchArticleViewModel>()
    private val mFriendViewModel by viewModels<FriendViewModel>()
    private val mHotKeyViewModel by viewModels<HotKeyViewModel>()
    private val mTopViewModel by viewModels<ArticleTopViewModel>()
    private val mWxArticleViewModel by viewModels<WxArticleViewModel>()
    private val mWxArticleListViewModel by viewModels<WxArticleListViewModel>()
    private val mWxArticleSearchListViewModel by viewModels<WxArticleSearchListViewModel>()
    private val mListProjectViewModel by viewModels<ListProjectViewModel>()
    private val mUserArticleViewModel by viewModels<UserArticleViewModel>()

    override fun init() {
        initView()
        initData()
    }

    private fun initData() {
        mViewModel.mLiveData.observeState(this) {
            onSuccess {
                LogUtils.d("--userInfo--", it?.datas.toString())
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mKnowViewModel.mKnowLiveData.observeState(this) {
            onSuccess { it ->
                it.forEach {
                    LogUtils.d("--knowInfo--", it?.name.toString())
                }
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mBannerViewModel.mBannerLiveData.observeState(this) {
            onSuccess { it ->
                it.forEach {
                    LogUtils.d("--bannerInfo--", it.imagePath.toString())
                }
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mProjectViewModel.mProjectLiveData.observeState(this) {
            onSuccess { it ->
                it.forEach {
                    LogUtils.d("--ProjectInfo--", it?.name.toString())
                }
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mProjectListViewModel.mProjectListLiveData.observeState(this) {
            onSuccess { it ->
                it.datas.forEach {
                    LogUtils.d("--ProjectInfo--", it.chapterName.toString())
                }
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mNaviVideModel.mNavListData.observeState(this) {
            onSuccess { it ->
                it.forEach {
                    it.articles.forEach {
                        LogUtils.d("--NaviInfo--", it.chapterName.toString())
                    }
                }
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mSearchViewModel.mSearchLiveData.observeState(this) {
            onSuccess {
                it.forEach {
                    it.datas.forEach {
                        LogUtils.d("--SearchInfo--", it.chapterName.toString())
                    }
                }
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mFriendViewModel.mFriendLiveData.observeState(this) {
            onSuccess { it ->
                it.forEach{
                    LogUtils.d("--friendInfo--", it.name.toString())
                }
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mHotKeyViewModel.mHotLiveData.observeState(this){
            onSuccess { it ->
                it.forEach{
                    LogUtils.d("--hotKeyInfo--", it.name.toString())
                }
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mTopViewModel.mArticleTopLiveData.observeState(this){
            onSuccess { it ->
                it.forEach{
                    LogUtils.d("--topInfo--", it.author.toString())
                }
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mWxArticleViewModel.mWxArticleLiveData.observeState(this){
            onSuccess { it ->
                it.forEach{
                    LogUtils.d("--wxArticleInfo--", it.name.toString())
                }
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mWxArticleListViewModel.mWxArticleListLiveData.observeState(this){
            onSuccess {
                LogUtils.d("--userInfo--", it.datas.toString())
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mWxArticleSearchListViewModel.mWxArticleSearchLiveData.observeState(this){
            onSuccess {
                LogUtils.d("--searchInfo--", it.datas.toString())
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mListProjectViewModel.mListProjectLiveData.observeState(this){
            onSuccess {
                LogUtils.d("--listProjectInfo--", it.datas.toString())
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
        mUserArticleViewModel.mUserArticleLiveData.observeState(this){
            onSuccess {
                LogUtils.d("--userArticleInfo--", it.datas.toString())
            }
            onException {
                LogUtils.e("--errorInfo--", it.message.toString())
            }
        }
    }

    private fun initView() {
        mBinding.tvLogin.setOnClickListener {
            // startActivity<LoginActivity>()
           // mViewModel.artList(1)
            mUserArticleViewModel.userArticle(1)
        }
        //mKnowViewModel.getKnowLedge()
        //mBannerViewModel.getBanner()
        // mProjectViewModel.getProject()
        // mProjectListViewModel.projectList(1,294)
        //mNaviVideModel.navi()
       // mSearchViewModel.searchArticle(1, "鸿洋")
        //mFriendViewModel.friend()
       // mHotKeyViewModel.hotKey()
       // mTopViewModel.topArticle()
     //   mWxArticleViewModel.wxArticle()
            //mWxArticleListViewModel.articleList(1,"408")
        //mWxArticleSearchListViewModel.wxArticleSearchList(1,"408","Android")
       // mListProjectViewModel.listProject(0)
    }

}